//
//  AY_AudioProgressView.m
//  AiYue
//
//  Created by mac on 2023/11/13.
//

#import "AY_AudioProgressView.h"

@interface AY_AudioProgressView ()

@property (nonatomic, assign) CGRect tempFrame;

@property (nonatomic, strong) UIView *backGaryView;//背景视图

@property (nonatomic, strong) UIView *backLoadProgressView;//加载进度视图

@property (nonatomic, strong) UIView *backGreenView;//已播放进度颜色视图

@property (nonatomic, assign) CGFloat backViewY;

@property (nonatomic, assign) CGFloat backViewH;

@property (nonatomic, assign) CGFloat playTimeW;//时间视图宽度

@property (nonatomic, assign) SlideType slideType;

@property (nonatomic, assign) int totalLength;

@end

@implementation AY_AudioProgressView

- (instancetype)initWithFrame:(CGRect)frame withType:(SlideType)typeStyle
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tempFrame = frame;
        self.backViewH = YNUM(2);
        self.backViewY = (self.tempFrame.size.height - 1)/2.0f;
        self.slideType = typeStyle;
        self.playTimeW = typeStyle == NormalStyle?YNUM(12):YNUM(50);
        [self addSubview:self.backGaryView];
        [self addSubview:self.backLoadProgressView];
        [self addSubview:self.backGreenView];
        [self addSubview:self.playTimeLabel];
        
    }
    return self;
}

- (UIView *)backGaryView
{
    if (!_backGaryView) {
        _backGaryView = [[UIView alloc] initWithFrame:CGRectMake(0, self.backViewY, self.tempFrame.size.width, self.backViewH)];
        _backGaryView.layer.masksToBounds = YES;
        _backGaryView.layer.cornerRadius = self.backViewH/2.0f;
        _backGaryView.backgroundColor = COLOR(@"F5F5F5");
    }
    return _backGaryView;
}

- (UIView *)backLoadProgressView
{
    if (!_backLoadProgressView) {
        _backLoadProgressView = [[UIView alloc] initWithFrame:CGRectMake(0, self.backViewY, 0, self.backViewH)];
        _backLoadProgressView.layer.masksToBounds = YES;
        _backLoadProgressView.layer.cornerRadius = self.backViewH/2.0f;
        _backLoadProgressView.backgroundColor = COLOR(@"F5F5F5");
    }
    return _backLoadProgressView;
}

- (UIView *)backGreenView
{
    if (!_backGreenView) {
        _backGreenView = [[UIView alloc] initWithFrame:CGRectMake(0, self.backViewY, 0, self.backViewH)];
        _backGreenView.layer.masksToBounds = YES;
        _backGreenView.layer.cornerRadius = self.backViewH/2.0f;
        _backGreenView.backgroundColor = COLOR(@"Ff5443");
    }
    return _backGreenView;
}

- (UILabel *)playTimeLabel
{
    if (!_playTimeLabel) {
        if (self.slideType == NormalStyle) {
            _playTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (CGRectGetHeight(self.frame) - self.playTimeW)/2, self.playTimeW, self.playTimeW)];
            _playTimeLabel.backgroundColor = COLOR(@"2F9AFB");
            _playTimeLabel.layer.borderColor = COLOR(@"ABD7FF").CGColor;
            _playTimeLabel.layer.borderWidth = YNUM(2);
            _playTimeLabel.layer.masksToBounds = YES;
            _playTimeLabel.layer.cornerRadius = self.playTimeW / 2.0f;
            _playTimeLabel.text = @"";
            UIPanGestureRecognizer * panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                                    action:@selector(doMoveAction:)];
            // 拖动功能暂时取消
            _playTimeLabel.userInteractionEnabled = NO;
            [_playTimeLabel addGestureRecognizer:panGestureRecognizer];
        }else{
            _playTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (CGRectGetHeight(self.frame) - CGRectGetHeight(self.tempFrame))/2, self.playTimeW, self.tempFrame.size.height)];
            _playTimeLabel.font = YFont(12);
            _playTimeLabel.textColor = COLOR(@"2E2E30");
            _playTimeLabel.text = @"1%";
            _playTimeLabel.textAlignment = NSTextAlignmentCenter;
            _playTimeLabel.backgroundColor = [UIColor whiteColor];
            [self setViewshadowWith:_playTimeLabel andWithCornerRadius:self.tempFrame.size.height / 2.0f withColor:RGB(0, 0, 0, 0.1).CGColor withShadowOffSet:CGSizeMake(0, YNUM(1))];
            _playTimeLabel.layer.masksToBounds = YES;
            _playTimeLabel.layer.borderColor = COLOR(@"EEF0F1").CGColor;
            _playTimeLabel.layer.borderWidth = 0.5;
            UIPanGestureRecognizer * panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                                                    action:@selector(doMoveAction:)];
            // 拖动功能暂时取消
            _playTimeLabel.userInteractionEnabled = NO;
            [_playTimeLabel addGestureRecognizer:panGestureRecognizer];
        }
    }
    return _playTimeLabel;
}

- (void)setViewshadowWith:(UIView *)view andWithCornerRadius:(CGFloat)radius withColor:(CGColorRef)color withShadowOffSet:(CGSize)offset
{
    view.layer.cornerRadius = radius;
    view.layer.shadowColor = color;
    view.layer.shadowOffset = offset;
    view.layer.shadowOpacity = 1;
    view.layer.shadowRadius = 5;
}

-(void)doMoveAction:(UIPanGestureRecognizer *)recognizer{
    
    CGPoint translation = [recognizer translationInView:self];
    CGPoint newCenter = CGPointMake(recognizer.view.center.x+ translation.x,
                                    recognizer.view.center.y + translation.y);
    //    限制屏幕范围：
    newCenter.y = MAX(recognizer.view.frame.size.height/2, recognizer.view.frame.size.width/2);
    newCenter.y = MIN(self.frame.size.height - recognizer.view.frame.size.height/2, recognizer.view.frame.size.width/2);
    newCenter.x = MAX(recognizer.view.frame.size.width/2, newCenter.x);
    newCenter.x = MIN(self.frame.size.width - recognizer.view.frame.size.width/2,newCenter.x);
    recognizer.view.center = newCenter;
    [recognizer setTranslation:CGPointZero inView:self];
    CGRect greenRect = self.backGreenView.frame;
    greenRect.size.width = newCenter.x;
    self.backGreenView.frame = greenRect;
    
    CGFloat totalW = self.tempFrame.size.width - self.playTimeW;
    CGFloat moveW = newCenter.x - recognizer.view.frame.size.width/2;
    int moveX = moveW/totalW*self.totalLength;
    // 判断是垂直移动还是水平移动
    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:{ // 开始移动
            
            NSLog(@"====================开始移动");
        }
            break;
        case UIGestureRecognizerStateChanged:{ // 正在移动
            
            NSLog(@"====================正在移动");
            if ([self.delegate respondsToSelector:@selector(changePlayTimeByPublicAudioPlayProgressView:)]) {
                [self.delegate changePlayTimeByPublicAudioPlayProgressView:moveX];
            }
        }
            break;
        case UIGestureRecognizerStateEnded:{ // 移动停止
            // 移动结束也需要判断垂直或者平移
            NSLog(@"====================移动停止");
            if ([self.delegate respondsToSelector:@selector(changePlayTimeByPublicAudioPlayProgressViewEnd:)]) {
                [self.delegate changePlayTimeByPublicAudioPlayProgressViewEnd:moveX];
            }
            
            
        }
            break;
        case UIGestureRecognizerStateCancelled: {
            NSLog(@"====================移动取消");
        }
        default:
            break;
    }
    
    
}

#pragma mark - UIGestureRecognizerDelegate


- (void)changeCanPlayAbleProgress:(int)progressFloat totalLength:(int)totalCount
{
    if (progressFloat <= 0) {
        return;
    }
    if (totalCount <= 0) {
        return;
    }
    self.totalLength = totalCount;
    CGFloat moveX = (self.tempFrame.size.width - self.playTimeW)/totalCount;
    CGRect greenRect = self.backLoadProgressView.frame;
    greenRect.origin.x = moveX*progressFloat;
    self.backLoadProgressView.frame = greenRect;
}

- (void)changePlayProgress:(int)progressFloat totalLength:(int)totalCount
{

    if (totalCount <= 0) {
        return;
    }
    self.totalLength = totalCount;
    NSString *strShowTime = [NSString stringWithFormat:@"%.1f%%",(float)progressFloat*100/totalCount];
    CGFloat moveX = (self.tempFrame.size.width - self.playTimeW)/totalCount;
    CGRect timeRect = self.playTimeLabel.frame;
    timeRect.origin.x = moveX*progressFloat;
    self.playTimeLabel.frame = timeRect;
    
    CGRect greenRect = self.backGreenView.frame;
    greenRect.size.width = timeRect.origin.x;
    self.backGreenView.frame = greenRect;
    if (self.slideType == NormalStyle) {
        self.playTimeLabel.text = @"";
    }else{
        self.playTimeLabel.text = strShowTime;
    }
    
}

+ (instancetype)publicAudioPlayProgressView:(CGRect)frame withType:(SlideType)typeStyle
{
    AY_AudioProgressView *view = [[AY_AudioProgressView alloc] initWithFrame:frame withType:typeStyle];
    return view;
}

@end
